package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomBean;
import com.project.mapper.ICustomMapper;
import com.project.service.ICustomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CustomServiceImpl implements ICustomService {
    @Autowired
    private ICustomMapper customMapper;
    @Override
    public IPage<CustomBean> cutFindByItem(Integer pageNum, String name, String scope) {
        QueryWrapper<CustomBean> qw = new QueryWrapper<>();
        if (name != null && name !="") {
            qw.like("c_name", name);
        }
        if (scope != null && scope !="") {
            qw.like("c_range", scope);
        }
        return customMapper.selectPage(new Page<>(pageNum,3),qw);
    }

    @Override
    public CustomBean findById(Integer customId) {
        return customMapper.selectById(customId);
    }

    @Override
    public void updateById(Integer customId, String newHead, String newTel, String newScope) {
        CustomBean customBean = customMapper.selectById(customId);
        customBean.setHead(newHead);
        customBean.setTel(newTel);
        customBean.setScope(newScope);
        customMapper.updateById(customBean);
    }

    @Override
    public CustomBean findByName(String customName) {
        QueryWrapper<CustomBean> qw = new QueryWrapper<>();
        qw.eq("c_name",customName);
        return customMapper.selectOne(qw);
    }

    @Override
    public void add(CustomBean customBean) {
        customMapper.insert(customBean);
    }

    @Override
    public List<CustomBean> findAll() {
        QueryWrapper<CustomBean> qw = new QueryWrapper();
        return customMapper.selectList(qw);
    }
}
